package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Duration;
import java.util.Set;

public class SeleniumTest2 {
    public static void main(String[] args) throws IOException {
        // 设置 ChromeDriver 的路径
        System.setProperty("webdriver.chrome.driver", "D:\\dcloud-software\\software\\anti-fraud\\src\\main\\resources\\chrome-driver\\chromedriver.exe");

        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.addArguments("--user-agent=Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Mobile Safari/537.36");
        chromeOptions.addArguments("--window-size=480,800");

        WebDriver driver = new ChromeDriver(chromeOptions);

        try {
            // 访问应用首页 URL
            driver.get("https://yjeju.rq9eyvt.store/s97b1gyl?_360safeparam=29903015"); // 替换为实际的应用首页 URL

            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(1000));

            WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='安装']")));
            // 成功找到元素后的操作...
            // 找到并点击安装按钮
            element.click();

            // 获取所有窗口句柄
            String mainWindowHandle = driver.getWindowHandle();
            Set<String> allWindowHandles = driver.getWindowHandles();

            // 切换到新窗口
            for (String handle : allWindowHandles) {
                if (!handle.equals(mainWindowHandle)) {
                    driver.switchTo().window(handle);
                    break;
                }
            }

            // 获取 APK 文件的下载链接
            WebElement apkLinkElement = driver.findElement(By.tagName("a")); // 替换为实际的链接定位方式
            String apkUrl = apkLinkElement.getAttribute("href");

            // 下载 APK 文件并保存到本地指定目录
            downloadFile(apkUrl, "C:/path/to/save/downloaded.apk");

        } finally {
            // 关闭浏览器
            if (driver != null) {
                driver.quit();
            }
        }
    }

    public static void downloadFile(String fileURL, String saveDir) throws IOException {
        URL url = new URL(fileURL);
        URLConnection connection = url.openConnection();
        int contentLength = connection.getContentLength();

        try (BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
             FileOutputStream out = new FileOutputStream(saveDir)) {
            byte[] dataBuffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
                out.write(dataBuffer, 0, bytesRead);
            }
        }
    }
}

